package de.backtomedieval.activities.startscreen;

import java.util.ArrayList;

import de.backtomedieval.R;
import de.backtomedieval.activities.TopBar;
import de.backtomedieval.activities.gamemenu.Gamemenu;
import de.backtomedieval.activities.gamemenu.ScenarioMenu;
import de.backtomedieval.buildings.AbstractBuilding;
import de.backtomedieval.game.Player;
import de.backtomedieval.main.Game;
import de.backtomedieval.scenarios.FreeGame;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ProfileStartscreen extends Activity {
	
	private ListView lv1;
	private AbstractBuilding toBuild = null;
	private ArrayList<String> m_profilemenu;
    private ProfileMenuAdapter m_profilemenuadapter;
	private Handler mHandler = new Handler();
	private boolean lastState = false;
	private TextView tv_buildinfo;
	private LinearLayout layoutChoose;
	private RelativeLayout layoutInfo;
	private int refresh = Game.getRefreshrate();
	private TextView title;
	private TopBar topbar;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profilemenu);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		Game.setLastActivity(this);


		lv1=(ListView)findViewById(R.id.ListViewProfileMenu);
        m_profilemenu = new ArrayList<String>();
        this.m_profilemenuadapter = new ProfileMenuAdapter(this, R.layout.row, m_profilemenu);
        lv1.setAdapter(this.m_profilemenuadapter);

        m_profilemenuadapter.add(Game.getString(R.string.tutorial));
    	m_profilemenuadapter.add(Game.getString(R.string.campagne));
    	m_profilemenuadapter.add(Game.getString(R.string.freegame));
    	m_profilemenuadapter.add(Game.getString(R.string.scenario));
    	m_profilemenuadapter.notifyDataSetChanged();
        
        lv1.setTextFilterEnabled(true);
 
        lv1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            	onProfileScreenChoose(position);
            }
        });
    	
    	

	}

	@Override
	public void onBackPressed() {
		Intent changeToIntent = new Intent(this,Startscreen.class);
		startActivity(changeToIntent);
		this.finish();
		return;
	}
	
	
	private class ProfileMenuAdapter extends ArrayAdapter<String> {

	    private ArrayList<String> items;

	    public ProfileMenuAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
	            super(context, textViewResourceId, items);
	            this.items = items;
	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	            View v = convertView;
	            if (v == null) {
	                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	                v = vi.inflate(R.layout.row, null);
	            }
	            String o = items.get(position);
	            if (o != null) {
	                    TextView tt = (TextView) v.findViewById(R.id.toptext);
	                    TextView bt = (TextView) v.findViewById(R.id.bottomtext);
	                    ImageView icon=(ImageView)v.findViewById(R.id.icon);
	                    icon.setImageResource(R.drawable.icon);
	                    tt.setText(o);
	                    bt.setText("");
	            }
	            return v;
	    }
	}

	protected void onProfileScreenChoose(int position) {

		if (position == 0) {
			//Tutorial
			Game.showPopup("Coming soon...");
		} else if (position == 1) {
			Game.showPopup("Coming soon...");
		} else if (position == 2) {
			
			Game g = new Game();
			Player player = new Player(Game.getPlayername());
			player.addCity(Game.getCityname());
			g.addPlayer(player);

			Game.setStartScenario(new FreeGame());
			g.setScenario(Game.getStartScenario());
			g.getScenario().atGamestart();
			Game.startGame();			
			
			Intent newgameIntent = new Intent(ProfileStartscreen.this, Gamemenu.class);
			startActivity(newgameIntent);
			this.finish();
			

		} else if (position == 3) {
			Intent newscenarioIntent = new Intent(ProfileStartscreen.this,
					ScenarioMenu.class);
			startActivity(newscenarioIntent);
			this.finish();
		}
	}
}